package mybatis.testDemo.datasource;

import com.google.common.collect.Maps;
import mybatis.testDemo.MybatisBean;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.StandardEnvironment;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Map;

import static mybatis.testDemo.datasource.PgDataSource.pgDruidDataSource;

public class PluginDataSource {
    public static void setupSqlSessionFactoryForRoutePlugin(AnnotationConfigApplicationContext applicationContext) throws SQLException {
        DataSource pgDruidDataSource = pgDruidDataSource();
        Map<String, DataSource> map = Maps.newHashMap();
        map.put("pgDruidDataSource", pgDruidDataSource);
        map.put("druidDataSource", MybatisBean.MapperConfiguration.druidDataSource());

        StandardEnvironment environment = (StandardEnvironment) applicationContext.getEnvironment();
        GenericBeanDefinition definition = new GenericBeanDefinition();
        definition.setBeanClass(SqlSessionFactoryBean.class);
        // and new ConfigDataApplicationContextInitializer load propertiesSource,
        // CustomRoutingDataSource
        Interceptor interceptor = MybatisBean.MapperConfiguration.AddOrgCodeForPageInteceptor();
        Interceptor pageInterceptor = MybatisBean.MapperConfiguration.PageInterceptor();
        Interceptor sqlPrintInteceptor = MybatisBean.MapperConfiguration.SqlPrintInteceptor();
        // sql print
        Interceptor[] interceptors = {sqlPrintInteceptor,pageInterceptor, interceptor};
        String threadLocal = environment.getProperty("RouteDataSourceToTransformWithThreadLocal");
        if (threadLocal == null || threadLocal.equals("threadLocal")) {
            CustomRoutingDataSourceWithThreadLocal customRoutingDataSourceWithThreadLocal = CustomRouteDataSource.RouteDataSourceConfigurationDemo.customRoutingDataSourceWithThreadLocal(map);
            customRoutingDataSourceWithThreadLocal.afterPropertiesSet();
            definition.getPropertyValues().add("dataSource", customRoutingDataSourceWithThreadLocal);
        } else {
            CustomRoutingDataSource customRoutingDataSource = CustomRouteDataSource.RouteDataSourceConfigurationDemo.customRoutingDataSource(map);
            customRoutingDataSource.afterPropertiesSet();
            definition.getPropertyValues().add("dataSource", customRoutingDataSource);
        }
        org.springframework.core.io.ClassPathResource classPathResource = new org.springframework.core.io.ClassPathResource("sqlmap/MapperInterface.xml");
        definition.getPropertyValues().add("plugins", interceptors);
        definition.getPropertyValues().add("mapperLocations", classPathResource);
        applicationContext.registerBeanDefinition("sqlSessionFactory", definition);


    }
}
